<?php
class TestOfURLMapper extends UnitTestCase {

    function testParseNoRules() 
    {
        $this->expectException('Exception','sbook/urlmapper: no rules found!');
        $mapper = new urlmapper(null);
    }

    function testParseRules() 
    {

        $rules[] = array(
            'url'=>'',
            'map'=>array('app'=>'sbook','controller'=>'main','action'=>'index')
        );
        $rules[] = array(
            'url'=>':app/:controller/:action/*',
            'map'=>array()
        );
        $urlmapper = new urlmapper($rules);

        $request = $urlmapper->parse('');
        $this->assertEqual($request->app,'sbook');
        $this->assertEqual($request->controller,'main');
        $this->assertEqual($request->action,'index');
        $this->assertEqual($request->params,array());

        $request = $urlmapper->parse('myapp/mycontroller/myaction/1/2');
        $this->assertEqual($request->app,'myapp');
        $this->assertEqual($request->controller,'mycontroller');
        $this->assertEqual($request->action,'myaction');
        $this->assertEqual($request->params['param0'],'1');
        $this->assertEqual($request->params['param1'],'2');
        $this->assertNull($request->params['param3']);

        $request = $urlmapper->parse('myapp/mycontroller/myaction/1');
        $this->assertEqual($request->app,'myapp');
        $this->assertEqual($request->controller,'mycontroller');
        $this->assertEqual($request->action,'myaction');
        $this->assertEqual($request->params['param0'],'1');
        $this->assertNull($request->params['param1']);

        $request = $urlmapper->parse('xpto');
        $this->assertNull($request->app,'myapp');
        $this->assertNull($request->controller,'mycontroller');
        $this->assertNull($request->action,'myaction');
        $this->assertEqual($request->params,array());
    }
}
?>
